package com.androidhelper.module.common.http.interceptor;

import android.text.TextUtils;

import com.androidhelper.module.common.utils.SPUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
   @desc 设置请求头拦截器
 * 设置Http请求头。给OkHttp 添加请求头拦截器，配置请求头信息。还可以为接口统一添加请求头数据。
 * 例如，把用户名、密码（或者token）统一添加到请求头。后续每个接口的请求头中都会携带用户名、密码（或者token）数据，避免了为每个接口单独添加。

   添加header


   Language: zh_cn
   Platform-CU: Android
   Content-Type: application/x-www-form-urlencoded
   Content-Length: 120
   Host: aaa.mex.group:81
   Connection: Keep-Alive
   Accept-Encoding: gzip
   User-Agent: okhttp/3.10.0



   //    builder.addHeader("Device-Time", device_time);
   //        builder.addHeader("Device-Name", silentURLEncode(Build.MODEL));
   //        builder.addHeader("Device-Model", silentURLEncode(Build.MODEL));
   //        builder.addHeader("System-Name", "Android");
   //        builder.addHeader("System-Version", silentURLEncode(Build.VERSION.RELEASE));
   //        builder.addHeader("App-Version", AppUtil.getAppVersionName(MyApplication.getContext()));
   //        builder.addHeader("User-Platform", "android");
   //        builder.addHeader("Device-Product", silentURLEncode(Build.PRODUCT));
   //        builder.addHeader("Device-Fingerprint", silentURLEncode(Build.FINGERPRINT));
   //        builder.addHeader("Device-Hardware", silentURLEncode(Build.HARDWARE));
   //builder.addHeader("token", ConfigManager.getUserToken());


 */
public class HeaderInterceptor implements Interceptor {

    private HashMap<String, Object> headers;

    public HeaderInterceptor() {
    }

    public HeaderInterceptor(HashMap<String, Object> headers) {
        this.headers = headers;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder requestBuilder = request.newBuilder();
        String domain = request.url().host();
           //根据项目实际需求 添加 Headers
        requestBuilder.addHeader("Content-Type", "application/json;charset=utf-8")
                .addHeader("Connection", "keep-alive")
                .addHeader("Language", "zh_CN") //en_US todo 这块需要根据多语言进行国际化,先不做,等多语言事在做(参考mex2.0)
                .addHeader("Platform-CU", "Android")
                .header("Authorization", "token")
                .header("Accept-Encoding", "gzip")
                .header("Content-Length", "0");



        // 将 Cookie 设置在 header 中
        String cookie = SPUtils.getInstance().getString(domain);
        if (!TextUtils.isEmpty(domain) && !TextUtils.isEmpty(cookie)) {
            requestBuilder.addHeader("Cookie", cookie);
        }

        // 如果公共请求头不为空,则构建新的请求
        if (headers != null) {
            for (Map.Entry<String, Object> entry : headers.entrySet()) {
                requestBuilder.addHeader(entry.getKey(), entry.getValue().toString());
            }
        }

        Request requestB = requestBuilder.build();
        Response.Builder responseBuilder = chain.proceed(requestB).newBuilder();
        if (!TextUtils.isEmpty(requestB.cacheControl().toString())) {
            responseBuilder
                    .removeHeader("Pragma")
                    .removeHeader("Cache-Control")
                    .header("Cache-Control", "public, max-age=" + requestB.cacheControl().maxAgeSeconds());
        }
        return responseBuilder.build();
    }
}
